package com.leadnews.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.leadnews.mapper.CartoonCommentMapper;
import com.leadnews.mapper.CartoonContentMapper;
import com.leadnews.mapper.LevelMapper;
import com.leadnews.pojo.Cartoon;
import com.leadnews.pojo.CartoonComment;
import com.leadnews.pojo.CartoonContent;
import com.leadnews.pojo.Level;
import com.leadnews.service.CartoonCommentService;
import com.leadnews.service.CartoonService;
import com.leadnews.mapper.CartoonMapper;
import com.leadnews.vo.CartoonVo;
import com.leadnews.vo.CommentVo;
import com.leadnews.vo.ResponseResult;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.*;

/**
 * @author 自己
 * @description 针对表【cartoon】的数据库操作Service实现
 * @createDate 2023-01-01 09:51:41
 */
@Service
@Slf4j
public class CartoonServiceImpl extends ServiceImpl<CartoonMapper, Cartoon>
        implements CartoonService{
    @Autowired
    CartoonMapper cartoonMapper;
    @Autowired
    CartoonContentMapper cartoonContentMapper;
    @Override
    public ResponseResult getCartoon(String name) {
        ResponseResult result = new ResponseResult();
        Map<String, String> map = new HashMap<>();
        ArrayList<Object> arrayList = new ArrayList<>();
        LambdaQueryWrapper<Cartoon> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Cartoon::getName,name);
        try {
            Cartoon cartoon = cartoonMapper.selectOne(queryWrapper);
            LambdaQueryWrapper<CartoonContent> queryWrapper1 = new LambdaQueryWrapper<>();
            queryWrapper1.eq(CartoonContent::getCartoonId,cartoon.getId());
            arrayList.add(cartoon);
            List<CartoonContent> list = cartoonContentMapper.selectList(queryWrapper1);
            for (CartoonContent content : list) {

                arrayList.add(content.getEpisode());
            }
            result.setHost(list.get(0).getEpisodeLink());
            result.setCode(200);
            result.setData(arrayList);
        }catch (Exception e){
            log.info("番名未找到");
            result.setCode(0);
        }
        return result;
    }

    @Override
    public ResponseResult getCartoonNum(CartoonVo cartoonVo) {
        ResponseResult result = new ResponseResult();
        System.out.println(cartoonVo.getNum());
        System.out.println(cartoonVo.getCartoonname());
        LambdaQueryWrapper<CartoonContent> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(CartoonContent::getName,cartoonVo.getCartoonname())
                .eq(CartoonContent::getEpisode,cartoonVo.getNum());
        try {
            CartoonContent cartoonContent = cartoonContentMapper.selectOne(queryWrapper);
            result.setCode(200);
            result.setData(cartoonContent.getEpisodeLink());
        }catch (Exception e){
            log.info("没有这集");
            result.setCode(0);
        }
        return result;
    }
@Autowired
    CartoonCommentMapper cartoonCommentMapper;
    @Autowired
    CartoonCommentService cartoonCommentService;
    @Override
    public ResponseResult saveComment(CommentVo commentVo) {
        ResponseResult result = new ResponseResult();
        if (StringUtils.isBlank(commentVo.getComment())){
            result.setCode(0);
            result.setErrorMessage("输入不能为空");
            return result;
        }
        LambdaQueryWrapper<Cartoon> cartoonLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cartoonLambdaQueryWrapper.eq(Cartoon::getName,commentVo.getCartoonname());
        try {
            Cartoon cartoon = cartoonMapper.selectOne(cartoonLambdaQueryWrapper);
            CartoonComment cartoonComment = new CartoonComment();
            cartoonComment.setComment(commentVo.getComment());
            cartoonComment.setCartoonId(cartoon.getId()+"");
            cartoonComment.setUserId(commentVo.getUserId());
            String format = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date());
            cartoonComment.setTime(format);
            boolean save = cartoonCommentService.save(cartoonComment);
            if (save){
                result.setCode(200);
                result.setErrorMessage("发布评论成功");
            }
        }catch (Exception e){
            result.setCode(0);
        }
        return result;
    }
@Autowired
    LevelMapper levelMapper;
    @Override
    public ResponseResult findComment(Map<String, String> map) {
        String cartoonname = map.get("cartoonname");
        ResponseResult result = new ResponseResult();
        ArrayList<CommentVo> cartoonVos = new ArrayList<>();
        LambdaQueryWrapper<Cartoon> cartoonLambdaQueryWrapper = new LambdaQueryWrapper<>();
        LambdaQueryWrapper<CartoonComment> cartoonCommentLambdaQueryWrapper = new LambdaQueryWrapper<>();
        cartoonLambdaQueryWrapper.eq(Cartoon::getName,cartoonname);
        try {
            Cartoon cartoon = cartoonMapper.selectOne(cartoonLambdaQueryWrapper);
            cartoonCommentLambdaQueryWrapper.eq(CartoonComment::getCartoonId,cartoon.getId());
            cartoonCommentLambdaQueryWrapper.orderByDesc(CartoonComment::getTime);
            List<CartoonComment> list = cartoonCommentMapper.selectList(cartoonCommentLambdaQueryWrapper);

            for (CartoonComment cartoonComment : list) {
                Level level = levelMapper.selectById(cartoonComment.getUserId());
                CommentVo cartoonVo = new CommentVo();
                cartoonVo.setLevel(level.getStar());
                cartoonVo.setLike(cartoonComment.getLike1());
                cartoonVo.setName(level.getName());
                cartoonVo.setText(cartoonComment.getComment());
                cartoonVo.setTime(cartoonComment.getTime());
                cartoonVo.setUrl(level.getPicture());
                cartoonVo.setUnlike(cartoonComment.getUnlike());
                cartoonVos.add(cartoonVo);
                }
            result.setCode(200);
            result.setData(cartoonVos);
log.info(String.valueOf(list));
        }catch (Exception e){
            result.setCode(0);
        }
        return result;
    }

    @Override
    public ResponseResult findCartoon() {
        ResponseResult result = new ResponseResult();
        try {
            List<Cartoon> list = cartoonMapper.findAll();
            result.setCode(200);
            result.setData(list);
        }catch (Exception e){
            result.setCode(0);
        }
        return result;
    }
}




